<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社区uid" prop="communityUid">
      <el-input v-model="dataForm.communityUid" placeholder="社区uid"></el-input>
    </el-form-item>
    <el-form-item label="创建人的uid" prop="createUserUid">
      <el-input v-model="dataForm.createUserUid" placeholder="创建人的uid"></el-input>
    </el-form-item>
    <el-form-item label="创建人名称" prop="createUserName">
      <el-input v-model="dataForm.createUserName" placeholder="创建人名称"></el-input>
    </el-form-item>
    <el-form-item label="管理员uid" prop="adminUid">
      <el-input v-model="dataForm.adminUid" placeholder="管理员uid"></el-input>
    </el-form-item>
    <el-form-item label="审核人名称" prop="auditName">
      <el-input v-model="dataForm.auditName" placeholder="审核人名称"></el-input>
    </el-form-item>
    <el-form-item label="审核拒绝原因" prop="rejectReason">
      <el-input v-model="dataForm.rejectReason" placeholder="审核拒绝原因"></el-input>
    </el-form-item>
    <el-form-item label="审核时间" prop="auditTime">
      <el-input v-model="dataForm.auditTime" placeholder="审核时间"></el-input>
    </el-form-item>
    <el-form-item label="审核状态，0：未审核，1：审核拒绝，2：审核通过" prop="auditStatus">
      <el-input v-model="dataForm.auditStatus" placeholder="审核状态，0：未审核，1：审核拒绝，2：审核通过"></el-input>
    </el-form-item>
    <el-form-item label="动态内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="动态内容"></el-input>
    </el-form-item>
    <el-form-item label="是否发布：0：否，1：是" prop="isPublish">
      <el-input v-model="dataForm.isPublish" placeholder="是否发布：0：否，1：是"></el-input>
    </el-form-item>
    <el-form-item label="文件uid列表" prop="fileUids">
      <el-input v-model="dataForm.fileUids" placeholder="文件uid列表"></el-input>
    </el-form-item>
    <el-form-item label="话题uid列表" prop="topicUids">
      <el-input v-model="dataForm.topicUids" placeholder="话题uid列表"></el-input>
    </el-form-item>
    <el-form-item label="URL链接" prop="url">
      <el-input v-model="dataForm.url" placeholder="URL链接"></el-input>
    </el-form-item>
    <el-form-item label="URL链接信息" prop="urlInfo">
      <el-input v-model="dataForm.urlInfo" placeholder="URL链接信息"></el-input>
    </el-form-item>
    <el-form-item label="点击数" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击数"></el-input>
    </el-form-item>
    <el-form-item label="评论数" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder="评论数"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否开启评论" prop="openComment">
      <el-input v-model="dataForm.openComment" placeholder="是否开启评论"></el-input>
    </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          communityUid: '',
          createUserUid: '',
          createUserName: '',
          adminUid: '',
          auditName: '',
          rejectReason: '',
          auditTime: '',
          auditStatus: '',
          content: '',
          isPublish: '',
          fileUids: '',
          topicUids: '',
          url: '',
          urlInfo: '',
          clickCount: '',
          commentCount: '',
          remark: '',
          status: '',
          createTime: '',
          updateTime: '',
          openComment: '',
          sort: ''
        },
        dataRule: {
          communityUid: [
            { required: true, message: '社区uid不能为空', trigger: 'blur' }
          ],
          createUserUid: [
            { required: true, message: '创建人的uid不能为空', trigger: 'blur' }
          ],
          createUserName: [
            { required: true, message: '创建人名称不能为空', trigger: 'blur' }
          ],
          adminUid: [
            { required: true, message: '管理员uid不能为空', trigger: 'blur' }
          ],
          auditName: [
            { required: true, message: '审核人名称不能为空', trigger: 'blur' }
          ],
          rejectReason: [
            { required: true, message: '审核拒绝原因不能为空', trigger: 'blur' }
          ],
          auditTime: [
            { required: true, message: '审核时间不能为空', trigger: 'blur' }
          ],
          auditStatus: [
            { required: true, message: '审核状态，0：未审核，1：审核拒绝，2：审核通过不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '动态内容不能为空', trigger: 'blur' }
          ],
          isPublish: [
            { required: true, message: '是否发布：0：否，1：是不能为空', trigger: 'blur' }
          ],
          fileUids: [
            { required: true, message: '文件uid列表不能为空', trigger: 'blur' }
          ],
          topicUids: [
            { required: true, message: '话题uid列表不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: 'URL链接不能为空', trigger: 'blur' }
          ],
          urlInfo: [
            { required: true, message: 'URL链接信息不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击数不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '评论数不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          openComment: [
            { required: true, message: '是否开启评论不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/community/usermoment/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.communityUid = data.userMoment.communityUid
                this.dataForm.createUserUid = data.userMoment.createUserUid
                this.dataForm.createUserName = data.userMoment.createUserName
                this.dataForm.adminUid = data.userMoment.adminUid
                this.dataForm.auditName = data.userMoment.auditName
                this.dataForm.rejectReason = data.userMoment.rejectReason
                this.dataForm.auditTime = data.userMoment.auditTime
                this.dataForm.auditStatus = data.userMoment.auditStatus
                this.dataForm.content = data.userMoment.content
                this.dataForm.isPublish = data.userMoment.isPublish
                this.dataForm.fileUids = data.userMoment.fileUids
                this.dataForm.topicUids = data.userMoment.topicUids
                this.dataForm.url = data.userMoment.url
                this.dataForm.urlInfo = data.userMoment.urlInfo
                this.dataForm.clickCount = data.userMoment.clickCount
                this.dataForm.commentCount = data.userMoment.commentCount
                this.dataForm.remark = data.userMoment.remark
                this.dataForm.status = data.userMoment.status
                this.dataForm.createTime = data.userMoment.createTime
                this.dataForm.updateTime = data.userMoment.updateTime
                this.dataForm.openComment = data.userMoment.openComment
                this.dataForm.sort = data.userMoment.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/community/usermoment/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'communityUid': this.dataForm.communityUid,
                'createUserUid': this.dataForm.createUserUid,
                'createUserName': this.dataForm.createUserName,
                'adminUid': this.dataForm.adminUid,
                'auditName': this.dataForm.auditName,
                'rejectReason': this.dataForm.rejectReason,
                'auditTime': this.dataForm.auditTime,
                'auditStatus': this.dataForm.auditStatus,
                'content': this.dataForm.content,
                'isPublish': this.dataForm.isPublish,
                'fileUids': this.dataForm.fileUids,
                'topicUids': this.dataForm.topicUids,
                'url': this.dataForm.url,
                'urlInfo': this.dataForm.urlInfo,
                'clickCount': this.dataForm.clickCount,
                'commentCount': this.dataForm.commentCount,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'openComment': this.dataForm.openComment,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
